import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { SystemUserStateType } from './typings'
import {
  getSystemUserList,
  getBelongTenantSelectList,
  addSystemUserInfo,
  updateSystemUserInfo,
  deleteSystemUserInfo,
  updateSystemUserRoleGrant,
  resetSystemUserPassword,
  exportSystemUserList
} from '~/api/system/systemUser'
import { filterValueEnum, DownloadAttachment } from '~/utils/util'

const userSexSelectList: SystemUserStateType['userSexSelectList'] = [
  {
    key: 1,
    value: '男'
  },
  {
    key: 2,
    value: '女'
  },
  {
    key: 3,
    value: '未知'
  }
]

const systemUser: Module<SystemUserStateType, SystemUserStateType> = {
  namespaced: true,
  state: {
    belongTenantSelectList: [],
    belongTenantEnum: {},
    userSexSelectList,
    userSexEnum: filterValueEnum(userSexSelectList, 'key', 'value')
  },
  getters: {
    belongTenantSelectList: (state) => state.belongTenantSelectList,
    belongTenantEnum: (state) => state.belongTenantEnum,
    userSexSelectList: (state) => state.userSexSelectList,
    userSexEnum: (state) => state.userSexEnum
  },
  mutations: {
    SET_BELONG_TENANT_SELECT_LIST (state, payload) {
      state.belongTenantSelectList = payload
      state.belongTenantEnum = filterValueEnum(payload, 'tenantId', 'tenantName')
    }
  },
  actions: {
    // 获取列表信息
    async GetSystemUserList<S, T> (_: ActionContext<S, T>, params: T) {
      return await getSystemUserList<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 导出用户管理列表
    async ExportSystemUserList<S, T> (_: ActionContext<S, T>, params: T) {
      return await exportSystemUserList<T>(params).then((res: AxiosResponse) => {
        const contentDisposition = res?.headers?.['content-disposition']
        if (contentDisposition && res.data) {
          DownloadAttachment(decodeURI(contentDisposition.substring(20)), res.data)
        }
        return res.data
      })
    },
    // 添加用户管理项
    async AddSystemUserInfo<S, T> (_: ActionContext<S, T>, params: T) {
      return await addSystemUserInfo<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 修改用户管理项
    async UpdateSystemUserInfo<S, T> (_: ActionContext<S, T>, params: T) {
      return await updateSystemUserInfo<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 删除用户管理项
    async DeleteSystemUserInfo<S, T> (_: ActionContext<S, T>, params: T) {
      return await deleteSystemUserInfo<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 修改用户角色配置
    async UpdateSystemUserRoleGrant<S, T> (_: ActionContext<S, T>, params: T) {
      return await updateSystemUserRoleGrant<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 重置用户密码
    async ResetSystemUserPassword<S, T> (_: ActionContext<S, T>, params: T) {
      return await resetSystemUserPassword<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 获取所属租户下拉列表
    async GetBelongTenantSelectList<S, T> ({ commit }: ActionContext<S, T>, params: T) {
      return await getBelongTenantSelectList<T>(params).then((res: AxiosResponse) => {
        commit('SET_BELONG_TENANT_SELECT_LIST', res?.data?.data || [])
        return res?.data
      })
    }
  }
}

export default systemUser
